<script setup>

import { computed, onMounted, reactive } from "vue";
import { ArrowRight } from "@element-plus/icons-vue";
import { onBeforeRouteUpdate, useRoute, useRouter } from "vue-router";
import { useStore } from "vuex";
import { ElMessage, ElMessageBox } from "element-plus";
import hxxp from "@/utils/http";

const data = reactive({
  tabIco: false,
  dynamicBreadcrumb: [], //动态面包屑,
  systemUser: {},
  systemMenus: []
})

const router = useRouter();

const sys = sessionStorage.getItem('systemUser')
if (sys != null && sys != '') {
  data.systemUser = JSON.parse(sys)
} else {
  ElMessage({
    message: '登录已过期，请重新登录',
    type: 'error'
  })
  router.push('/login')
}

const getMenus = () => {
  hxxp.get('/systemMenu/getMenu/' + data.systemUser.id).then(res => {
    data.systemMenus = res.data
  })
}
getMenus()
// 退出系统函数
const logout = () => {
  ElMessageBox.confirm('确定要退出系统吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    // 清除用户登录状态
    sessionStorage.removeItem('systemUser')

    // 清除store中的用户信息和标签
    store.commit('clearCurrentUser')
    store.commit('clearTags')

    ElMessage.success('退出成功');
    // 跳转到登录页面
    router.push('/');
  }).catch(() => {
    // 用户点击取消
    ElMessage.info('已取消退出');
  });
}
//动态面包屑
onMounted(() => {

  router.afterEach(route => {
    //与给定路由地址匹配的标准化的路由记录数组。
    const matchedRoutes = route.matched;
    const dynamicBreadcrumb = []
    for (const route of matchedRoutes) {
      if (route.meta && route.meta.breadcrumb) {
        dynamicBreadcrumb.push({
          name: route.meta.breadcrumb,
          path: route.path
        })
      }
    }
    //赋值
    data.dynamicBreadcrumb = dynamicBreadcrumb
  })
})

const handleOpen = (key, keyPath) => {
  console.log(key, keyPath)
}
const handleClose = (key, keyPath) => {
  console.log(key, keyPath)
}
// //切换左侧菜单
// const toggleMenu = (b) => {
//
// }


// ====================>>>> 动态标签开始 ======================

const route = useRoute();
const store = useStore();
// 获取vuex里面所有的tag
const tagsList = computed(() => store.state.tagsList)
// 关闭一个标签
const closeTags = index => {
  //获取删除的标签的信息
  const delItem = tagsList.value[index];

  //让vuex删除指定的标签
  store.commit('delTagsItem', {index});

  // 如果关闭的是当前标签且标签不止一个，则激活前一个标签，如果关闭的是不是当前标签且标签不止一个，则激活后一个标签
  const item = tagsList.value[index] ? tagsList.value[index] : tagsList.value[index - 1];
  if (item) {
    // 如果删除的是当前标签，则跳往上面item判断好的目标标签的地址
    if (isActive(delItem.path)) {
      router.push(item.path)
    }
  } else {
    // 如果tagsList只有一个标签，删除了标签就没有标签，则跳转到首页
    router.push("/");
  }
}

// 设置标签
const setTags = (route) => {
  const isExist = tagsList.value.some(item => item.path === route.fullPath)
  if (!isExist) {
    // tag超过5个就删除第一个
    if (tagsList.value.length >= 10) {
      store.commit('delTagsItem', {index: 1})
    }
    store.commit('setTagsItem', {
      name: route.name,
      path: route.fullPath,
      title: route.meta.title
    })
  }
}

//首次加载页面的时候便添加标签页
setTags(route);

// 监听路由变化，在当前页面即将要离开的时候触发
onBeforeRouteUpdate((to) => {
  setTags(to)
})

// 关全部标签
const closeAll = () => {
  store.commit('clearTags');
  router.push("/");
}

// 关闭其它标签
const closeOther = () => {
  const curItem = tagsList.value.filter(item => {
    return item.path === route.fullPath;
  })
  store.commit('clearTagsOther', curItem);
}
// 显示标签栏
const showTags = computed(() => tagsList.value.length > 0)

// 判断tag是否被激活,如果被激活就高亮当前标签
const isActive = path => path === route.fullPath


// ====================>>>> 动态标签结束 ======================

</script>

<template>
  <div class="common-layout" >
    <el-container class="container">
      <el-aside class="aside" >
        <div class="logo">
          <!--          经典案例工程-->
          <img src="@/assets/images/hemiao.png" width="40" height="40">
          <div v-if="!data.tabIco" style="flex: 1;text-align: left">农业保险</div>
        </div>
        <div class="menu">
          <el-menu active-text-color="#1890ff" background-color="#304156" :collapse="data.tabIco"
                   class="el-menu-vertical-demo" default-active="2" text-color="#fff" @open="handleOpen"
                   @close="handleClose"
                   router>
            <el-menu-item index="/welcome" style="background-color: #304156">
              <el-icon>
                <setting/>
              </el-icon>
              <span>首页</span>
            </el-menu-item>
            <el-sub-menu v-for="(item, index) in data.systemMenus" :key="index" :index="index + 1">
              <template #title>
                <el-icon>
                  <component :is="item.menuIcon"></component>
                </el-icon>
                <span>{{ item.menuName }}</span>
              </template>
              <el-menu-item v-for="subItem in item.systemMenus" :key="subItem.id" :index="subItem.menuUrl">
                <el-icon>
                  <component :is="subItem.menuIcon"/>
                </el-icon>
                {{ subItem.menuName }}
              </el-menu-item>
            </el-sub-menu>
            <!-- <el-sub-menu index="1">
              <template #title>
                <el-icon>
                  <UploadFilled />
                </el-icon>
                <span>险种管理</span>
              </template>
              <el-menu-item index="/coverageList"><el-icon>
                  <Document />
                </el-icon>险种信息</el-menu-item>
            </el-sub-menu>
            <el-sub-menu index="2">
              <template #title>
                <el-icon>
                  <UploadFilled />
                </el-icon>
                <span>投保管理</span>
              </template>
              <el-menu-item index="/insureList"><el-icon>
                  <Document />
                </el-icon>投保信息</el-menu-item>
            </el-sub-menu>
            <el-sub-menu index="3">
              <template #title>
                <el-icon>
                  <UploadFilled />
                </el-icon>
                <span>报案管理</span>
              </template>
              <el-menu-item index="/claimList"><el-icon>
                  <Document />
                </el-icon>报案信息</el-menu-item>
            </el-sub-menu>
            <el-sub-menu index="4">
              <template #title>
                <el-icon>
                  <UploadFilled />
                </el-icon>
                <span>用户管理</span>
              </template>
              <el-menu-item index="/userList"><el-icon>
                  <Document />
                </el-icon>用户信息</el-menu-item>
              <el-menu-item index="/roleList"><el-icon>
                  <Document />
                </el-icon>角色信息</el-menu-item>
              <el-menu-item index="/menuList"><el-icon>
                  <Document />
                </el-icon>菜单信息</el-menu-item>
            </el-sub-menu> -->
          </el-menu>
        </div>
      </el-aside>
      <el-container>
        <el-header class="header">
          <div class="info">
            <div class="tab" @click="data.tabIco = !data.tabIco">
              <el-icon v-if="data.tabIco" size="25" color="#1f2d3d" style="margin-top: 13px">
                <Expand/>
              </el-icon>
              <el-icon v-if="!data.tabIco" size="25" color="#1f2d3d" style="margin-top: 13px">
                <Fold/>
              </el-icon>
            </div>
            <div class="breadcrumb">
              <el-breadcrumb :separator-icon="ArrowRight">
                <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
                <!--动态面包屑-->
                <el-breadcrumb-item v-for="(item, index) in data.dynamicBreadcrumb" :key="index">{{
                    item.name
                  }}
                </el-breadcrumb-item>
              </el-breadcrumb>
            </div>
            <div class="user-info">
              <el-dropdown>
                <span class="el-dropdown-link">
                  <el-avatar :size="40" style="margin-top: 5px"
                             src="https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png"/>
                  <el-icon style="margin-left: 5px"><arrow-down/></el-icon>
                </span>
                <template #dropdown>
                  <el-dropdown-menu>
                    <el-dropdown-item>个人中心</el-dropdown-item>
                    <el-dropdown-item>修改密码</el-dropdown-item>
                    <el-dropdown-item divided @click="logout">退出系统</el-dropdown-item>
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </div>
            <!--            <div class="photo">-->
            <!--              <el-avatar :size="40" style="margin-top: 5px" src="https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png" />-->
            <!--            </div>-->
            <div class="select"></div>
          </div>
          <div class="tagList" v-if="showTags">
            <ul>
              <li v-for="(item, index) in tagsList" :class="{ 'active': isActive(item.path) }" :key="index">
                <router-link :to="item.path" class="tag">
                  {{ item.title }}
                </router-link>
                <el-icon v-if="item.title != '首页'" @click="closeTags(index)" class="close">
                  <Close style="font-size: 11px;"/>
                </el-icon>
              </li>
            </ul>
          </div>
        </el-header>
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<style scoped>
/* 主容器 */
.container {
  height: 100vh;
  background: #f5f7fa;
}

/* 侧边栏 */
.aside {
  background: linear-gradient(180deg, #2c3e50 0%, #34495e 100%);
  box-shadow: 2px 0 8px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
}

/* Logo区域 */
.logo {
  width: 100%;
  height: 60px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  color: white;
  font-size: 16px;
  font-weight: 600;
  text-align: center;
  line-height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, 0.05);
  backdrop-filter: blur(10px);
}

.logo img {
  margin-right: 10px;
  border-radius: 8px;
  transition: transform 0.3s ease;
}

.logo img:hover {
  transform: scale(1.1);
}

/* 菜单区域 */
.menu {
  height: calc(100vh - 61px);
  overflow-y: auto;
}

.menu::-webkit-scrollbar {
  width: 4px;
}

.menu::-webkit-scrollbar-track {
  background: transparent;
}

.menu::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.2);
  border-radius: 2px;
}

/* 头部区域 */
.header {
  height: 80px;
  background: #fff;
  border-bottom: 1px solid #e4e7ed;
  padding: 0;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.info {
  width: 100%;
  height: 50px;
  display: flex;
  align-items: center;
  background: #fff;
}

/* 折叠按钮 */
.tab {
  width: 50px;
  text-align: center;
  height: 50px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s ease;
  border-radius: 4px;
  margin-left: 10px;
}

.tab:hover {
  background-color: #f5f7fa;
  cursor: pointer;
}

/* 面包屑 */
.breadcrumb {
  flex: 1;
  height: 50px;
  display: flex;
  align-items: center;
  padding-left: 20px;
}

.el-breadcrumb {
  height: 50px;
  line-height: 50px;
}

/* 用户信息 */
.user-info {
  width: 120px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 20px;
}

.el-dropdown-link {
  display: flex;
  align-items: center;
  cursor: pointer;
  color: #606266;
  padding: 8px 12px;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.el-dropdown-link:hover {
  background-color: #f5f7fa;
  color: #409eff;
}

/* 动态标签 */
.tagList {
  height: 40px;
  line-height: 40px;
  border-bottom: 1px solid #e4e7ed;
  padding-left: 20px;
  background: linear-gradient(90deg, #f8f9fa 0%, #fff 100%);
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.05);
}

.tagList ul {
  list-style: none;
  margin: 0;
  padding: 0;
}

.tagList ul li {
  float: left;
  margin: 6px 4px 0 0;
  height: 28px;
  border: 1px solid #e4e7ed;
  line-height: 28px;
  border-radius: 6px;
  text-align: center;
  background-color: #fff;
  color: #666;
  transition: all 0.3s ease;
  position: relative;
}

.tagList ul li:hover {
  border-color: #409eff;
  color: #409eff;
}

.tagList ul li.active {
  background: linear-gradient(135deg, #409eff 0%, #66b1ff 100%);
  border-color: #409eff;
  color: #fff;
  box-shadow: 0 2px 8px rgba(64, 158, 255, 0.3);
}

.tag {
  margin: 0 12px;
  height: 100%;
  font-size: 13px;
  text-decoration: none;
  color: inherit;
  display: block;
  padding: 0 8px;
}

.close {
  position: absolute;
  right: 4px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 12px;
  opacity: 0.6;
  transition: all 0.3s ease;
}

.close:hover {
  color: #f56c6c;
  opacity: 1;
  cursor: pointer;
}

/* 菜单样式优化 */
.el-menu-vertical-demo {
  border-right: none;
  background: transparent;
}

.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
}

.el-aside {
  width: auto;
}

.el-menu-item {
  background-color: transparent !important;
  color: #bdc3c7 !important;
  margin: 2px 8px;
  border-radius: 6px;
  transition: all 0.3s ease;
}

.el-menu-item:hover {
  background-color: rgba(255, 255, 255, 0.1) !important;
  color: #fff !important;
}

.el-menu-item.is-active {
  background: linear-gradient(135deg, #409eff 0%, #66b1ff 100%) !important;
  color: #fff !important;
  box-shadow: 0 2px 8px rgba(64, 158, 255, 0.3);
}

.el-sub-menu .el-menu-item {
  margin: 1px 4px;
}

.el-sub-menu__title {
  color: #bdc3c7 !important;
  margin: 2px 8px;
  border-radius: 6px;
  transition: all 0.3s ease;
}

.el-sub-menu__title:hover {
  background-color: rgba(255, 255, 255, 0.1) !important;
  color: #fff !important;
}

/* 主内容区域 */
.el-main {
  background: #f5f7fa;
  padding: 20px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .breadcrumb {
    padding-left: 10px;
  }

  .user-info {
    width: 80px;
    margin-right: 10px;
  }

  .tagList {
    padding-left: 10px;
  }
}

/* 动画效果 */
.fade-in {
  animation: fadeIn 0.5s ease-in;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}
</style>